PriorityQueue

Ecng.Collections

Represents a min priority queue.

实现: ICollection<ValueTuple<T, T>>, IEnumerable<ValueTuple<T, T>>, IEnumerable, IQueue<ValueTuple<T, T>>

构造函数

PriorityQueue
public PriorityQueue(Func<TPriority, TPriority, TPriority> subtractAbs, IComparer<TPriority> comparer)
priorityQueue = PriorityQueue(subtractAbs, comparer)

Represents a min priority queue.

subtractAbs
The function that calculates the absolute difference between two priorities.
comparer
Custom comparer dictating the ordering of elements. Uses Default if the argument is .
PriorityQueue
public PriorityQueue(Func<TPriority, TPriority, TPriority> subtractAbs)
priorityQueue = PriorityQueue(subtractAbs)

Initializes a new instance of the PriorityQueue class.

属性

Comparer
public IComparer<TPriority> Comparer { get; }
value = priorityQueue.Comparer

Gets the priority comparer used by the PriorityQueue.

Count
public int Count { get; }
value = priorityQueue.Count

Gets the number of elements contained in the PriorityQueue.

方法

Clear
public void Clear()
priorityQueue.Clear()

Removes all items from the PriorityQueue.

Dequeue
public ValueTuple<TPriority, TElement> Dequeue()
result = priorityQueue.Dequeue()

Removes and returns the minimal element from the PriorityQueue.

返回值: The minimal element of the PriorityQueue.

DequeueEnqueue
public TElement DequeueEnqueue(TPriority priority, TElement element)
result = priorityQueue.DequeueEnqueue(priority, element)

Removes the minimal element and then immediately adds the specified element with associated priority to the PriorityQueue,

priority
The priority with which to associate the new element.
element
The element to add to the PriorityQueue.

返回值: The minimal element removed before performing the enqueue operation.

Enqueue
public void Enqueue(TPriority priority, TElement element)
priorityQueue.Enqueue(priority, element)

Adds the specified element with associated priority to the PriorityQueue.

priority
The priority with which to associate the new element.
element
The element to add to the PriorityQueue.
EnqueueDequeue
public TElement EnqueueDequeue(TPriority priority, TElement element)
result = priorityQueue.EnqueueDequeue(priority, element)

Adds the specified element with associated priority to the PriorityQueue, and immediately removes the minimal element, returning the result.

priority
The priority with which to associate the new element.
element
The element to add to the PriorityQueue.

返回值: The minimal element removed after the enqueue operation.

EnqueueRange
public void EnqueueRange(IEnumerable<ValueTuple<TPriority, TElement>> items)
priorityQueue.EnqueueRange(items)

Enqueues a sequence of element/priority pairs to the PriorityQueue.

items
The pairs of elements and priorities to add to the queue.
EnqueueRange
public void EnqueueRange(TPriority priority, IEnumerable<TElement> elements)
priorityQueue.EnqueueRange(priority, elements)

Enqueues a sequence of elements pairs to the PriorityQueue, all associated with the specified priority.

elements
The elements to add to the queue.
priority
The priority to associate with the new elements.
Peek
public ValueTuple<TPriority, TElement> Peek()
result = priorityQueue.Peek()

Returns the minimal element from the PriorityQueue without removing it.

返回值: The minimal element of the PriorityQueue.

TryDequeue
public bool TryDequeue(TElement element, TPriority priority)
result = priorityQueue.TryDequeue(element, priority)

Removes the minimal element from the PriorityQueue, and copies it to the parameter, and its associated priority to the parameter.

element
The removed element.
priority
The priority associated with the removed element.

返回值: if the element is successfully removed; if the PriorityQueue is empty.

TryPeek
public bool TryPeek(TElement element, TPriority priority)
result = priorityQueue.TryPeek(element, priority)

Returns a value that indicates whether there is a minimal element in the PriorityQueue, and if one is present, copies it to the parameter, and its associated priority to the parameter. The element is not removed from the PriorityQueue.

element
The minimal element in the queue.
priority
The priority associated with the minimal element.

返回值: if there is a minimal element; if the PriorityQueue is empty.

字段

_version
private int _version
value = priorityQueue._version

Version updated on mutation to help validate enumerators operate on a consistent state.